/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Action;

import com.webaccess.retailws.Bean.BrandBean;
import com.webaccess.retailws.Bean.CategoryBean;
import com.webaccess.retailws.Bean.ImageBean;
import com.webaccess.retailws.Bean.ProductBean;
import com.webaccess.retailws.Bean.UserInfoBean;
import com.webaccess.retailws.Delegate.AdminDelegate;
import com.webaccess.retailws.Delegate.CategoryDelegate;
import com.webaccess.retailws.Delegate.UserInfoDelegate;
import com.webaccess.retailws.Util.CategoryUtil;
import com.webaccess.retailws.Util.AdminUtil;
import com.webaccess.retailws.Util.CommonUtil;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author Owner
 */
public class AdminDispatchAction extends DispatchAction {

    private String SUCCESS = "success";
    CategoryUtil categUtil = new CategoryUtil();
    AdminDelegate admnDelegate = null;
    UserInfoDelegate userDelegate = null;
    CategoryBean categBean = null;
    BrandBean brandBeanObj = null;
    ProductBean prodBean = null;
    UserInfoBean userBean = null;
    ImageBean imagBean = null;
    AdminUtil admnUtil = null;
    CommonUtil commUtil = new CommonUtil();
    ActionMessages errors = null;
    CategoryDelegate categDelegate = new CategoryDelegate();

   

    public ActionForward showManageUserRoles(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        userDelegate = new UserInfoDelegate();
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            UserInfoBean[] roleBeanList = userDelegate.getRoles();
            request.setAttribute("rolesList", roleBeanList);
            UserInfoBean[] userBeanList = userDelegate.getAllUsers(0);
            request.setAttribute("userList", userBeanList);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "manageuserrolesshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showManageAdmin(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        userDelegate = new UserInfoDelegate();
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("statusList", commUtil.getStatus());
            UserInfoBean[] userBeanList = userDelegate.getAllUsers(2);
            request.setAttribute("userList", userBeanList);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "manageadminshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showManageHomePageImage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        admnDelegate = new AdminDelegate();
        try {
            commUtil.getTopRightListMap(request);

            ImageBean[] imageList = admnDelegate.getAllHomePageImage();
            request.setAttribute("homePageImageList", imageList);
            saveToken(request);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "managehomepageimageshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward updateHomePageImage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        admnDelegate = new AdminDelegate();
        admnUtil = new AdminUtil();
        errors = new ActionMessages();
        String homePageImageName = "", imageName = "", imageDetail = "", filePath = "";
        Long imageId = 0L;
        try {
            commUtil.getTopRightListMap(request);
            DynaActionForm imageDynaForm = (DynaActionForm) form;
            if (isTokenValid(request)) {
                imageId = Long.parseLong(request.getParameter("imageId"));
                imageName = String.valueOf(imageDynaForm.getString("imageName"));
                imageDetail = String.valueOf(imageDynaForm.getString("imageDetail"));
                FormFile homePageImage = (FormFile) imageDynaForm.get("homePageImage");
                homePageImageName = String.valueOf(imageDynaForm.getString("homePageImageName"));

                Pattern fileType = Pattern.compile("/.*\\.(gif)|(jpeg)|(jpg)|(png)$/");
                Matcher matchFileType = fileType.matcher(homePageImage.getContentType());
                if (imageName.equalsIgnoreCase("") || imageDetail.equalsIgnoreCase("")) {
                    errors.add("errHPImage", new ActionMessage("errors.brand.required"));
                } else if (homePageImage.getFileSize() != 0) {
                    if (!matchFileType.find()) {
                        errors.add("errHomePageImage", new ActionMessage("errors.homepageimage.filetype"));
                    } else if (homePageImage.getFileSize() > 1048576) { //1MB
                        errors.add("errHomePageImage", new ActionMessage("errors.homepageimage.size"));
                    } else {
                        filePath = getServlet().getServletContext().getRealPath("/") + "images";
                        homePageImageName = admnUtil.uploadFile(homePageImage, filePath);

                        SUCCESS = admnUtil.saveImage(imageId, imageName, homePageImageName, imageDetail);
                    }
                } else {

                    SUCCESS = admnUtil.saveImage(imageId, imageName, homePageImageName, imageDetail);
                }
                resetToken(request);
                saveErrors(request, errors);
                if (errors.isEmpty()) {
                    SUCCESS = "imagesuccess";
                } else {
                    SUCCESS = "updateimagefailure";
                }
            }
            saveToken(request);
        } catch (Exception ex) {
            SUCCESS = "updateimagefailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward saveHomePageImage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        admnDelegate = new AdminDelegate();
        admnUtil = new AdminUtil();
        errors = new ActionMessages();
        String homePageImageName = "", imageName = "", imageDetail = "", filePath = "";

        try {
            commUtil.getTopRightListMap(request);
            DynaActionForm imageDynaForm = (DynaActionForm) form;
            if (isTokenValid(request)) {
                imageName = String.valueOf(imageDynaForm.getString("imageName"));
                imageDetail = String.valueOf(imageDynaForm.getString("imageDetail"));
                FormFile homePageImage = (FormFile) imageDynaForm.get("homePageImage");
                homePageImageName = String.valueOf(imageDynaForm.getString("homePageImageName"));
                Pattern fileType = Pattern.compile("/.*\\.(gif)|(jpeg)|(jpg)|(png)$/");
                Matcher matchFileType = fileType.matcher(homePageImage.getContentType());
                if (imageName.equalsIgnoreCase("") || imageDetail.equalsIgnoreCase("")) {
                    errors.add("errHPImage", new ActionMessage("errors.brand.required"));
                } else if (homePageImage.getFileSize() != 0) {
                    if (!matchFileType.find()) {
                        errors.add("errHomePageImage", new ActionMessage("errors.homepageimage.filetype"));
                    } else if (homePageImage.getFileSize() > 1048576) { //1MB
                        errors.add("errHomePageImage", new ActionMessage("errors.homepageimage.size"));
                    } else {
                        filePath = getServlet().getServletContext().getRealPath("/") + "images";
                        homePageImageName = admnUtil.uploadFile(homePageImage, filePath);

                        SUCCESS = admnUtil.saveImage(0L, imageName, homePageImageName, imageDetail);
                    }
                } else {
                    homePageImageName = "ImageNotFound.jpg";
                    SUCCESS = admnUtil.saveImage(0L, imageName, homePageImageName, imageDetail);
                }
                resetToken(request);
                saveErrors(request, errors);
                if (errors.isEmpty()) {
                    SUCCESS = "imagesuccess";
                } else {
                    SUCCESS = "saveimagefailure";
                }
            }
            saveToken(request);
        } catch (Exception ex) {
            SUCCESS = "saveimagefailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showEditImage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        imagBean = new ImageBean();
        admnDelegate = new AdminDelegate();
        Long imageId = 0L;
        try {
            commUtil.getTopRightListMap(request);

            imageId = Long.parseLong(request.getParameter("imageId"));
            imagBean = admnDelegate.getImage(imageId);
            request.setAttribute("imageDetail", imagBean);
            ImageBean[] imageList = admnDelegate.getAllHomePageImage();
            request.setAttribute("homePageImageList", imageList);
            saveToken(request);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "edithomepageimageshow";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward deleteImage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        admnDelegate = new AdminDelegate();
        Long imageId = 0L;
        try {
            commUtil.getTopRightListMap(request);

            imageId = Long.parseLong(request.getParameter("imageId"));

            boolean isImageDeleted = admnDelegate.deleteImage(imageId);
            if (isImageDeleted == true) {
                SUCCESS = "imagesuccess";
            } else {
                SUCCESS = "deleteimagefailure";
            }
        } catch (Exception ex) {
            SUCCESS = "deleteimagefailure";
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showManageContent(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        admnDelegate = new AdminDelegate();
        commUtil.getTopRightListMap(request);

        SUCCESS = "showmanagecontent";
        return mapping.findForward(SUCCESS);
    }
}
